using System;
using System.Collections.Generic;
using Microsoft.Win32;

namespace ATMCommon.ATMConfig
{
    public class ATMConfigReg : ATMConfigBase
    {

        #region //////////////////// REG HELPER ////////////////////
        private bool GetRegValue(RegistryKey root, string path, string keyName, out RegistryValueKind kind, out object value)
        {
            kind = RegistryValueKind.Unknown;
            value = null;

            try
            {
                RegistryKey key = root.OpenSubKey(path);
                value = key.GetValue(keyName);
                kind = key.GetValueKind(keyName);

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        private string GetStringValue(string path, string key, string defaultValue)
        {
            object regValue = null;
            RegistryValueKind kind = RegistryValueKind.Unknown;

            if (GetRegValue(Registry.LocalMachine, path, key, out kind, out regValue))
            {
                if (kind == RegistryValueKind.String)
                {
                    defaultValue = regValue.ToString();
                }
            }

            return defaultValue;
        }

        private int GetIntValue(string path, string key, int defaultValue)
        {
            object regValue = null;
            RegistryValueKind kind = RegistryValueKind.Unknown;

            if (GetRegValue(Registry.LocalMachine, path, key, out kind, out regValue))
            {
                if (kind == RegistryValueKind.DWord)
                {
                    int valueParse;
                    if (int.TryParse(regValue.ToString(), out valueParse))
                    {
                        defaultValue = valueParse;
                    }
                }
            }

            return defaultValue;
        }

        private int GetIntValue(string path, string key, int defaultValue, int min, int max)
        {
            object regValue = null;
            RegistryValueKind kind = RegistryValueKind.Unknown;

            if (GetRegValue(Registry.LocalMachine, path, key, out kind, out regValue))
            {
                if (kind == RegistryValueKind.DWord )
                {
                    int valueParse;
                    if (int.TryParse(regValue.ToString(), out valueParse))
                    {
                        if (valueParse >= min && valueParse <= max)
                        {
                            defaultValue = valueParse;
                        }
                    }
                }
            }

            return defaultValue;
        }
        #endregion


        #region ////////////////////  PUBLIC IMPL //////////////////

        public override string GetStringValue(LATMConfigPath path, string key, string defaultValue)
        {
            object regValue = null;
            RegistryValueKind kind = RegistryValueKind.Unknown;

            if (GetRegValue(Registry.LocalMachine, LATMConfigDic[path], key, out kind, out regValue))
            {
                if (kind == RegistryValueKind.String)
                {
                    defaultValue = regValue.ToString();
                }
            }

            return defaultValue;
        }

        public override int GetIntValue(LATMConfigPath path, string key, int defaultValue)
        {
            object regValue = null;
            RegistryValueKind kind = RegistryValueKind.Unknown;

            if (GetRegValue(Registry.LocalMachine, LATMConfigDic[path], key, out kind, out regValue))
            {
                if (kind == RegistryValueKind.DWord)
                {
                    int valueParse;
                    if (int.TryParse(regValue.ToString(), out valueParse))
                    {
                        defaultValue = valueParse;
                    }
                }
            }

            return defaultValue;
        }

        public override int GetIntValue(LATMConfigPath path, string key, int defaultValue, int min, int max)
        {
            object regValue = null;
            RegistryValueKind kind = RegistryValueKind.Unknown;

            if (GetRegValue(Registry.LocalMachine, LATMConfigDic[path], key, out kind, out regValue))
            {
                if (kind == RegistryValueKind.DWord)
                {
                    int valueParse;
                    if (int.TryParse(regValue.ToString(), out valueParse))
                    {
                        if (valueParse >= min && valueParse <= max)
                        {
                            defaultValue = valueParse;
                        }
                    }
                }
            }

            return defaultValue;
        }

        public override bool Init()
        {
            LATMConfigDic.Add(LATMConfigPath.Config, "SOFTWARE\\LittleATM\\Config");
            LATMConfigDic.Add(LATMConfigPath.DeviceManager, "SOFTWARE\\LittleATM\\DeviceManager");
            LATMConfigDic.Add(LATMConfigPath.Hardware, "SOFTWARE\\LittleATM\\Hardware");
            LATMConfigDic.Add(LATMConfigPath.ModeManager, "SOFTWARE\\LittleATM\\ModeManager");

            return true;
        }
        #endregion
    }
}
